//
//  RequestTool.m
//  jingjing2.0
//
//  Created by apple on 16/4/28.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "RequestTool.h"
#import "Header.h"
@implementation RequestTool
+(void)requestDataWithType:(NSString *)type andUrlString:(NSString *)urlString andParameters:(NSDictionary *)dict andRequestFinished:(void(^)(id responses,NSError *error))block
{
    NSString *String=nil;
    if ([urlString hasPrefix:@"http"]) {
        String=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }else
    {
        String=[[NSString stringWithFormat:@"%@/%@",URL,urlString]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }
    //增加这几行代码；
    AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
    [securityPolicy setAllowInvalidCertificates:YES];
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
    [manager setSecurityPolicy:securityPolicy];
        // 设置超时时间
        manager.requestSerializer = [AFJSONRequestSerializer serializer];

        [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
        manager.requestSerializer.timeoutInterval = 20.f;
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain", @"text/javascript", @"text/json", @"text/html", nil];

        [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
        if ([type isEqualToString:@"post"]) {
            [manager POST:String parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
           
               block(responseObject,nil);
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
               block(nil,error);
            }];
        }else
        {
            [manager GET:String parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                block(responseObject,nil);
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                block(nil,error);
            }];
        }
}
@end
